package net.lanelife.casino.caipiao.dto.admin;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import net.lanelife.casino.caipiao.entity.BalanceChangeRecord;
import net.lanelife.casino.caipiao.entity.BalanceChangeRecord_;
import net.lanelife.casino.common.dto.support.GenericTable;
import net.lanelife.casino.common.entity.json.serialize.CurrencySerializer;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.Lists;

/**
 * @author Lane
 * 
 * 额度增减记录表格
 *
 * @param <DTO>
 */
public class BalanceChangeRecordTable<DTO> extends GenericTable<DTO, BalanceChangeRecord> {

	private static final long serialVersionUID = 1040006291770324347L;
	
	@JsonSerialize(using = CurrencySerializer.class)
	private Double amountIn = 0D;
	@JsonSerialize(using = CurrencySerializer.class)
	private Double amountOut = 0D;
	
	public BalanceChangeRecordTable() {}

	public BalanceChangeRecordTable(Double amountIn, Double amountOut) {
		super();
		this.amountIn = amountIn;
		this.amountOut = amountOut;
	}

	@Override
	public List<Selection<?>> buildExpression(Root<BalanceChangeRecord> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
		List<Selection<?>> columns = Lists.newArrayList();
		columns.add(builder.sum(builder.<Double>selectCase().when(builder.equal(root.get(BalanceChangeRecord_.direction), BalanceChangeRecord.Direction.IN), root.<Double>get(BalanceChangeRecord_.amount)).otherwise(0D)));
		columns.add(builder.sum(builder.<Double>selectCase().when(builder.equal(root.get(BalanceChangeRecord_.direction), BalanceChangeRecord.Direction.OUT), root.<Double>get(BalanceChangeRecord_.amount)).otherwise(0D)));
		return columns;
	}

	public Double getAmountIn() {
		return amountIn;
	}

	public void setAmountIn(Double amountIn) {
		this.amountIn = amountIn;
	}

	public Double getAmountOut() {
		return amountOut;
	}

	public void setAmountOut(Double amountOut) {
		this.amountOut = amountOut;
	}

}
